﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SchoolLibrary.Entity.Person;
using SchoolLibrary.Entity;
using SchoolLibrary.DataBase.DAO;

namespace SchoolLibrary.Desktop.Guide.Input
{
    public partial class PupilInputDialog : PersonInputDialog
    {
        private Pupil pupil;

        public PupilInputDialog() : base()
        {
            InitializeComponent();
            List<Class> classes = ClassDAO.getInstance().findAll();
            foreach (Class pupilClass in classes)
            {
                this.classComboBox.Items.Add(pupilClass);
            }
        }

        public PupilInputDialog(Pupil pupil) : this()
        {
            this.pupil = pupil;
            setPerson(pupil);
            setClass(pupil.PupilClass);
        }

        public Pupil Pupil
        {
            get { return pupil; }
        }

        protected override void okButton_Click(object sender, EventArgs e)
        {
            if (pupil == null)
            {
                pupil = new Pupil();
            }
            build(pupil);
            pupil.PupilClass = (Class) this.classComboBox.SelectedItem;
            this.Close();
        }

        private void setClass(Class pupilClass)
        {
            foreach (object obj in this.classComboBox.Items)
            {
                Class currecntClass = (Class)obj;
                if (currecntClass.Equals(pupilClass))
                {
                    this.classComboBox.SelectedItem = currecntClass;
                }
            }
        }
    }
}
